<!-- 
 * @description: 登录页面
 * @fileName: login.vue 
 * @author: xiuxiu.deng 
 * @date: 2024-06-20 16:16:55 
 * @后台人员:  
 * @path:  
 * @version: V1.0.5 
!-->

<script setup lang="ts">
import { reactive, ref } from 'vue'

import type { AxiosError, AxiosResponse } from 'axios'
import type { FormInstance, FormRules } from 'element-plus'
import { ElLoading, ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

import { useLoginStore } from '@/stores/login'
import api from '@/utils/api'

//登陆状态存储
const loginStore = useLoginStore()
//路由状态
const router = useRouter()

/**
 * 1.写登陆的界面组件实现
 *
 * 2.界面逻辑实现
 *
 * 3.登陆状态检测
 *
 */
//登陆表单数据
const loginForm = reactive({
  account: '',
  password: '',
  captcha: ''
})
//校验规则
const rules = reactive<FormRules<typeof loginForm>>({
  account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [
    {
      validator: (rule: any, value: any, callback: any) => {
        if (value === '') {
          callback(new Error('密码不能为空'))
        } else if (loginForm.password.length < 6) {
          callback(new Error('密码不能小鱼6位3字符'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  captcha: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
})
//form表单的实例
const formRef = ref<FormInstance>()
/**
 * 提交
 */
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((isValid: boolean) => {
    if (!isValid) {
      return
    }
    //显示加载动画
    const loading = ElLoading.service({
      lock: true,
      text: '正在登陆',
      background: 'rgba(0, 0, 0, 0.7)'
    })
    //请求接口，进行登陆
    api
      .post('/api/login', loginForm)
      .then((response: AxiosResponse<any>) => {
        if (response.status != 200 || !response.data || response.data.code != 200) {
          if (response.data) {
            ElMessage.error('登陆失败:' + response.data.msg)
          } else {
            ElMessage.error('登陆失败!')
          }
          return
        }
        let token = response.headers.authorization
        //存储token，后续使用
        loginStore.authorization = token
        router.push('/index')
      })
      .catch((error: AxiosError<any>) => {
        if (error.response && error.response.data) {
          ElMessage.error(error.response.data.msg)
          return
        }
        ElMessage.error('操作异常：' + error.message)
      })
      .finally(() => {
        loading.close()
      })
  })
}
// 验证码路径
const captchaSrc = ref('/api/login/captcha')
/**
 * 刷新验证码
 */
const changecaptchaSrc = () => {
  captchaSrc.value = '/api/login/captcha?t_=' + new Date().getTime()
}
</script>

<template>
  <div class="loginPage">
    <el-card class="loginPanel">
      <div class="loginPanelInner">
        <div class="logo">
          <img src="../../assets/images/logo.png" />
        </div>
        <el-divider direction="vertical" border-style="dashed" class="split" />
        <div class="loginForm">
          <div class="systemName">用户登陆</div>
          <el-form
            ref="formRef"
            size="large"
            :model="loginForm"
            status-icon
            :rules="rules"
            label-width="120px"
            class="form"
          >
            <el-form-item label="账号：" prop="account">
              <el-input
                v-model="loginForm.account"
                placeholder="请输入账号"
                autocomplete="off"
                suffix-icon="UserFilled"
              />
            </el-form-item>
            <el-form-item label="密码：" prop="password">
              <el-input
                v-model="loginForm.password"
                placeholder="请输入密码"
                type="password"
                autocomplete="off"
                suffix-icon="Lock"
              />
            </el-form-item>
            <el-form-item label="验证码：" prop="captcha">
              <div style="display: flex; width: 100%">
                <div style="flex: 1">
                  <el-input v-model.number="loginForm.captcha" placeholder="请输入验证码" />
                </div>
                <div class="captchaSrc">
                  <img :src="captchaSrc" @click="changecaptchaSrc" />
                </div>
              </div>
            </el-form-item>
            <div class="registerBtn">
              <el-link type="primary" href="/register" :underline="false">
                去账户？点击注册
              </el-link>
            </div>
            <el-form-item>
              <el-button type="primary" @click="submitForm(formRef)" class="loginBtn">
                登陆
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>
    <div class="footer">@Copyright 君君军通用管理系统 备案信息：陕432432432</div>
  </div>
</template>

<style scoped>
.loginPage {
  width: 100%;
  height: 100%;
  display: flex;
  justify-items: center;
  justify-content: center;
  align-items: center;
  background: linear-gradient(133deg, #1994bb, #19acbb, #19b4bb, #2a89db);
}

.loginPage .loginPanel {
  width: 60%;
  height: 60%;
  min-width: 600px;
  max-width: 1000px;
  min-height: 400px;
  max-height: 500px;
  margin: 0 auto;
}

.loginPage .loginPanel >>> .el-card__body {
  width: 100%;
  height: 100%;
}

.loginPage .loginPanel .loginPanelInner {
  width: 100%;
  height: 100%;
  display: flex;
}

.loginPage .loginPanel .loginPanelInner .logo {
  width: 40%;
  text-align: center;
  display: flex;
  justify-items: center;
  justify-content: center;
  align-items: center;
}

.loginPage .loginPanel .loginPanelInner .logo img {
  width: 50%;
}

.loginPage .loginPanel .loginPanelInner .split {
  height: calc(100% - 40px);
}

.loginPage .loginPanel .loginPanelInner .loginForm {
  flex: 1;
}

.loginPage .loginPanel .loginPanelInner .loginForm .systemName {
  text-align: center;
  font-size: 30px;
  line-height: 60px;
  letter-spacing: 3px;
  margin-bottom: 20px;
}

.loginPage .loginPanel .loginPanelInner .loginForm .form {
  width: 80%;
}

.loginPage .loginPanel .loginPanelInner .loginForm .form .loginBtn {
  width: 100%;
}

.loginPage .loginPanel .loginPanelInner .loginForm .form .captchaSrc {
  width: 100px;
  height: 100%;
  padding-left: 10px;
}

.loginPage .loginPanel .loginPanelInner .loginForm .form .captchaSrc img {
  width: 100px;
  height: 100%;
  cursor: pointer;
}

.loginPage .loginPanel .loginPanelInner .loginForm .form .registerBtn {
  text-align: right;
  line-height: 40px;
  margin-bottom: 5px;
}

.footer {
  position: fixed;
  bottom: 0px;
  line-height: 40px;
  text-align: center;
  font-size: 14px;
  color: #fff;
}
</style>
